<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
if($_RQ['op']=='list'){
	$return['levels2']=pdo_getall('jy_weishop_member_level',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['levels2'][]=array(
		'id'=>'0',
		'title'=>'默认会员',
		'level'=>0,
		'discount'=>'0',
		'ordermoney'=>'0.00',
		'ordercount'=>'0',
		'status'=>'1',
	);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=model('jy_merch/member')->member_show($item['mid'],$merchid);
			if(!$item['member']['id']){
				pdo_delete('jy_merch_member',array('mid'=>$item['mid'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));	
				unset($list[$k]);
			}
			if(!$item['fromuser']){
				$item['fromuser']=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
				pdo_update('jy_merch_member',array('fromuser'=>$item['fromuser']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			$item['shmember']=cfc('core')->member_show($item['fromuser']);
			$item['from_os']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'from_os');
			$item['from_os']=$item['from_os']?get_ostitle()[$item['from_os']]:'微信';
			$item['creditdata']=$this->model('member')->creditdata($item);
			$item['shopmid']=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
			$item['level']=pdo_getcolumn('jy_weishop_member',array('mid'=>$item['mid'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'level');
			$item['leveltitle']=pdo_getcolumn('jy_weishop_member_level',array('id'=>$item['level'],'uniacid'=>$_SESSION['uniacid']),'title')?:'默认等级';
			foreach($return['levels2'] as $k=>$lv){
				if($lv['id']==$item['level']){
					$item['levelkey']=$k;
				}
			}
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['nickname']=$item['member']['nickname'];
			$item['avatar']=$item['member']['avatar'];
			$item['mobile']=$item['member']['mobile'];
			$item['realname']=$item['member']['realname'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'
		<red>[温馨提示]：
		</br>1、每个商户有自己的会员体系（具体如何成为商户的会员，请移步基本设置--商户会员体系）；！</red>
		</br>2、需管理员平台设置好消息模板ID（移步：多商户总部后台--设置--基本设置--消息模板设置），才能有消息提醒！
		','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'shmember','title'=>'上级信息','type'=>'member'),
			array('name'=>'level','title'=>'等级','type'=>'select','options'=>'levels'),
			array('name'=>'creditdata','title'=>'积分/余额/虚拟币','type'=>'credit'),
			array('name'=>'from_os','title'=>'来源','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'sendcoupon','title'=>'发优惠券'),
			array('type'=>'modal','op'=>'sendmsg','title'=>'发消息'),
			array('type'=>'modal','op'=>'recharge','title'=>'充值'),
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
			array('type'=>'href','op'=>'edit','title'=>'编辑'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['levels']=pdo_getall('jy_weishop_member_level',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	//添加默认等级
	$return['levels'][0]=array(
		'id'=>'0',
		'title'=>'默认会员',
		'level'=>0,
		'discount'=>'0',
		'ordermoney'=>'0.00',
		'ordercount'=>'0',
		'status'=>'1',
	);
	//$return['htmls']=$htmls;
	$return['coupons']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));
	$return['credit_types']=credit_types();	
	$return['member_status']=$this->member_status;
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$fromuser=intval($post['fromuser']);
		if($fromuser==$mid){
			exi('推荐人不能是自己！','error');
		}
		$member=$this->get('member',array('mid'=>$fromuser,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		if(!$member){
			exi('指定推荐人不存在！','error');
		}
		$this->update('member',array('fromuser'=>$fromuser),array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('mid'=>$_RQ['mid'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('mid','fromuser'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('type'=>'choose','name'=>'fromuser','title'=>'设置推荐人','action'=>'jy_merch/get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='sendcoupon'){
	if($_RQ['postdata']){
		$data=get_postdata();
		$num=intval($data['num'])?:1;
		$mid=intval($data['mid']);
		if($num>20){
			exi('每人最多发放20张！','error');
		}
		if(!$mid){
			exi('请选择会员！','error');
		}
		$data1=array('mid'=>$mid,'merchid'=>$merchid,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
		$data2=array('mid'=>$mid,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'gettype'=>1,'createtime'=>TIMESTAMP);
		for($i=1;$i<=$num;$i++){
			pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$data['coupon']));
			pdo_insert('jy_weishop_coupon_getlog',$data2);
			pdo_insert('jy_weishop_coupon_log',$data1);
		}	
		$this->model('notice')->sendcoupon($mid);
		exi('发送成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=array('mid'=>$mid);
	$item['t_time'] =$item['t_time']?date('Y/m/d H:i',$item['t_time']):'';
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发送优惠券',
		'edit'=>array(
			array('type'=>'copy','name'=>'mid','title'=>'会员MID'),		
			array('type'=>'select','name'=>'coupon','title'=>'选择优惠券','options'=>'coupon'),			
			array('type'=>'string','name'=>'num','title'=>'每人发放数量','help'=>'此处受总数限制，如果剩余张数不足以发放给选定会员数量，则无法发放'),	
		)
	);
	$return['htmls']=$htmls;
	$return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='sendmsg'){
	if($_RQ['postdata']){
		$data=get_postdata();
		$mid=intval($data['mid']);
		if(!$data['content']){
			exi('请填写要发送的消息内容！','error');
		}
		if(!$mid){
			exi('请选择会员！','error');
		}
		$data1=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'merchid'=>$merchid,
			'status'=>0,
			'content'=>$data['content'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_msglog',$data1);
		$this->model('notice')->sendmsg($mid,$data['content']);
		exi('发送成功！','','//list/');
	}
	$mid=intval($_RQ['mid']);
	$item=array('mid'=>$mid);
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'发送消息',
		'edit'=>array(
			array('type'=>'copy','name'=>'mid','title'=>'会员MID'),		
			array('type'=>'text','name'=>'content','title'=>'消息内容'),	
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='recharge'){
	//充值
	$credit_types=credit_types();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['value']){
			exi('请先输入金额/积分！','error');
		}
		if(!$postdata['type']){
			exi('请选择类型！','error');
		}
		$typetitle=$credit_types[$postdata['type']];
		if($postdata['value'] <=0){
			exi($typetitle.'的值不能小于等于0','error');
		}
		if(is_numeric($postdata['value']) === false){	
			exi($typetitle.'的值必须是整数或小数','error');
		}
		if(!isset($postdata['is_add'])){
			exi('请选择增加/减少！','error');
		}
		$mid=intval($postdata['mid']);
		$type=trim($postdata['type']);
		$is_add=intval($postdata['is_add']);
		$value=trim($postdata['value']);
		$remark=trim($postdata['remark']);
		$member=pdo_get('jy_merch_member',array('mid'=>$mid,'merchid'=>$merchid),array('id','credit1','credit2','credit3','credit4','credit5','credit6'));
		if($is_add==0 && $value>$member['credit'.$type]){
			exi('请减少的['.$typetitle.']不能大于用户拥有的积分！','error');
		}
		//执行操作
		$this->model('member')->member_credit($merchid,$mid,'credit'.$type,$value,$is_add,'core',$remark,'',$_SESSION['uid']);
		exi('操作['.$typetitle.']成功！','','list');
	}
	$id=$_RQ['id']?intval($_RQ['id']):intval($_RQ['mid']);
	if(!$id){
		exi('请先选择要充值的会员！','error');
	}
	$member=pdo_get('jy_merch_member',array('mid'=>$id,'merchid'=>$merchid),array('mid','credit1','credit2','credit2','credit3','credit4','credit5','credit6'));
	$member['nickname']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'nickname');
	$return['item']=$member;
	$return['types']=credit_types();
	$return['is_adds']=array(
		'1'=>'增加',
		'0'=>'减少',
	);
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'会员昵称','read'=>true),
		),
	);
	foreach($credit_types as $k=>$cv){
		$htmls['edit'][]=array('type'=>'string','name'=>'credit'.$k,'title'=>'当前'.$cv,'read'=>true);
	}
	$htmls['edit'][]=array('type'=>'radio','name'=>'type','title'=>'积分类型','options'=>'types');
	$htmls['edit'][]=array('type'=>'radio','name'=>'is_add','title'=>'增加/减少','options'=>'is_adds');
	$htmls['edit'][]=array('type'=>'string','name'=>'value','title'=>'额度');
	$htmls['edit'][]=array('type'=>'text','name'=>'remark','title'=>'备注');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$mid=intval($_RQ['mid']);
	if($mid){
		$item=pdo_get('jy_weishop_member',array('merchid'=>$merchid,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$item['order_count']=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid'],'status'=>3));
		$sql = "SELECT SUM(price) FROM " . tablename('jy_weishop_order') . " WHERE uniacid=:uniacid AND mid=:mid AND status=3";
		$money = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":mid" => $item['mid']));
		$item['order_money']=$money['SUM(price)'];
		$member=model('core/member')->detail($item['mid'],array('nickname','avatar','mobile','realname'));
		$item['nickname']=$member['nickname'];
		$item['avatar']=$member['avatar'];
		$item['mobile']=$member['mobile'];
		$item['realname']=$member['realname'];
		$item['openid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'openid');
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回会员列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'会员昵称','name'=>'nickname','help'=>''),
					array('type'=>'copy','title'=>'OPENID','name'=>'openid'),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'select','title'=>'会员等级','name'=>'level','options'=>'levels','help'=>''),
					array('type'=>'string','title'=>'真实姓名','name'=>'realname','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'注册时间','name'=>'createtime','help'=>'','read'=>true),
					array('type'=>'switch','title'=>'加入黑名单','name'=>'isblack','help'=>'设置黑名单后，此会员无法访问商城'),
				)
			),
				array(
				'title'=>'交易信息',
				'list'=>array(
					array('type'=>'string','title'=>'成交订单数','name'=>'order_count','help'=>'','read'=>true),
					array('type'=>'string','title'=>'成交额','name'=>'order_money','help'=>'','read'=>true),
				)
			),
		)
	);
	
	$return['group']=pdo_getall('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid']));
	$return['levels']=pdo_getall('jy_weishop_member_level',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'level'=>intval($postdata['level']),
		'isblack'=>intval($postdata['isblack']),
	);
	$member=array(
		'nickname'=>trim($postdata['nickname']),
		'avatar'=>trim($postdata['avatar']),
		'realname'=>trim($postdata['realname']),
	);
	$id=$postdata['shopmid']?:intval($postdata['id']);
	$mid=intval($postdata['mid']);
	if($id){
		pdo_update('jy_weishop_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		pdo_update('core_members',$member,array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		pdo_insert('jy_weishop_member',$data);
	}
	exi('操作成功！','','list');
}
?>